package com.merugu.school.spring;

import com.merugu.school.beans.UserBean;
import com.merugu.school.hibernate.User;
import com.merugu.school.util.SpringUtil;
import org.hibernate.Transaction;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import javax.mail.Session;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Data Access Object to manipulate user related data
 */
public class UserDao extends HibernateDaoSupport {

    /**
     * Logger instance for this class
     */
    private static Logger LOGGER = Logger.getLogger(UserDao.class.getName());

    /**
     * Spring bean name for this class
     */
    private static final String NAME = "userDao";

    public UserDao() {
    }

    /**
     * Checks the user credentials against the given user name and password.
     * After successful login. it returns the UserBean otherwise returns null
     *
     * @param userName
     * @param password
     * @return
     */
    public UserBean login(String userName, String password) {
        LOGGER.info("Performing the login with the user" + userName);
        List list = getHibernateTemplate().findByExample(new User(userName, password));

        if (list != null && list.size() > 0) {
            User user = (User) list.get(0);
            if (user != null) {
                return SpringUtil.convert(user, true);
            }
        }
        return null;
    }

    /**
     * Inserts new  user into the system
     *
     * @param user
     */
    public void insertUser(UserBean user) {
        LOGGER.info("Inserting new user into .....");

        try {
            User userToBe = SpringUtil.convert(user);
            getHibernateTemplate().save(userToBe);

        } catch (DataAccessException e) {
            LOGGER.log(Level.SEVERE, "inserting the user is failed..", e);
        }
    }


    public static UserDao getUserDao() {
        return SpringUtil.getBean(NAME, UserDao.class);
    }

    /**
     * Updates the user
     *
     * @param userBean
     */
    public void updateUser(UserBean userBean) {
        User user = SpringUtil.convert(userBean);
        getHibernateTemplate().update(user);

    }

    /**
     * Deletes the user
     *
     * @param userBean
     */
    public void deleteUser(UserBean userBean) {
        LOGGER.info("Deleting the user" + userBean.getUserName());
        User user = SpringUtil.convert(userBean);
        getHibernateTemplate().delete(user);

    }

    /**
     * Returns  the list of available users in the system.
     *
     * @return
     */
    public List<UserBean> getUsers() {
        LOGGER.info("Listing the users in the system.");
        List<User> users = getHibernateTemplate().loadAll(User.class);
        List<UserBean> userBeans = new ArrayList<UserBean>();
        for (User user : users) {
            userBeans.add(SpringUtil.convert(user, false));

        }

        return userBeans;
    }
}
